/* pb
Michael Zahniser, 09/23/09

Console program to copy or paste from the clipboard.
*/

#include "_paste/Pasteboard.h"

#include <iostream>

using std::cin;
using std::cout;
using std::endl;



int main(int argc, char *argv[])
{
	bool doHelp = false;
	bool doPaste = false;
	bool doCopy = false;
	
	for(char **it = argv + 1; *it; ++it)
	{
		if(**it == '-')
			switch((*it)[1]) {
				case 'h': doHelp = true; break;
				case 'v': doPaste = true; break;
				case 'c': doCopy = true; break;
				default: break;
			}
	}
	
	if(doHelp)
		cout << "Pasteboard command line tool. Options:" << endl
			<< "-h: Display this help message." << endl
			<< "-v (default): Print the pasteboard to stdout." << endl
			<< "-c: Copy stdin into the pasteboard." << endl;
	
	doPaste |= !(doHelp | doCopy);
	if(doPaste)
		cout << Pasteboard::Get();
	
	if(doCopy)
	{
		string str;
		static size_t BUFFER_SIZE = 1024;
		char buffer[BUFFER_SIZE];
		
		do {
			cin.read(buffer, BUFFER_SIZE);
			str.append(buffer, cin.gcount());
		} while(cin);
		
		Pasteboard::Set(str);
	}
	
	return 0;
}
